<!--
 * @Author: Zhong admin@51y.cc
 * @Description: 商城订单
 * @FilePath: /tenement-admin/src/views/workOrder/mallOrder/index.vue
 * @LastEditTime: 2024-12-26 10:22:18
 * @LastEditors: Zhong
-->

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="小区" prop="communityId">
            <communitySelect v-model="queryParams.communityId" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="订单编号" prop="orderNo">
            <el-input
              v-model="queryParams.orderNo"
              placeholder="请输入订单编号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择状态"
              clearable
            >
              <el-option
                v-for="(value, key) of statusMap"
                :key="key"
                :label="value"
                :value="key"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户ID" prop="communityMemberId">
            <el-input
              v-model="queryParams.communityMemberId"
              placeholder="请输入用户ID"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
    
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      >
      <template #left>
          <el-button icon="Download" @click="showExportDialog"
            >导出</el-button
          >
        </template>
    </right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallOrderList">
      <el-table-column label="ID" align="center" prop="id" width="50" />

      <el-table-column label="订单信息" prop="orderInfo" min-width="200">
        <template v-slot="scope">
          <orderInfo
            :orderId="scope.row.orderNo"
            :detail="scope.row.mallOrderDetailsList[0] || {}"
          />
        </template>
      </el-table-column>
      <el-table-column label="小区ID" align="center" prop="communityId">
        <template v-slot="scope">
          <span>{{ matchCommunity(scope.row.communityId).name || "-" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="支付金额" align="center" prop="payPrice" />

      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="180"
      >
      </el-table-column>
      <el-table-column label="服务时间" align="center" prop="serviceTime">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <span>{{ statusMap[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template v-slot="scope">
          <el-button type="text" @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            v-if="scope.row.status === 20 && scope.row.refundStatus !== 1"
            type="text"
            @click="handleRefund(scope.row)"
            >退款</el-button
          >
          <el-button
            v-if="
              scope.row.refundStatus !== 1 &&
              scope.row.payStatus === 1 &&
              scope.row.status !== 40
            "
            type="text"
            @click="handlePayEnd(scope.row)"
            >核销</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <orderDetail v-model="detailDialog" v-if="detailDialog" :row="curRow" />

    <el-dialog title="导出" v-model="dialogVisible">
      <el-form ref="exportFormRef" :model="exportForm" label-width="80px">
        <el-form-item label="导出日期" prop="time">
          <el-date-picker
            v-model="exportForm.time"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            format="YYYY-MM-DD HH:mm:ss"
            :default-time="[
              new Date(2000, 1, 1, 0, 0, 0),
              new Date(2000, 2, 1, 23, 59, 59),
            ]"
          />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <span class="dialog-footer">
          <el-button @click="showExportDialog(false)">取 消</el-button>
          <el-button type="primary" @click="exportPropertyPayedFee1"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import communitySelect from "@/components/communitySelect";
import orderDetail from "./orderDetail.vue";
import orderInfo from "./orderInfo.vue";
import {
  getMallOrderPage,
  applyRefund,
  verificationOrder,
  exportPropertyBill1,
} from "@/api/workOrder";
import { matchCommunity } from "@/common";

const { proxy } = getCurrentInstance();
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const mallOrderList = ref([]);
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  communityId: "",
  orderNo: "",
  status: "",
  communityMemberId: "",
});

const dialogVisible = ref(false);
const exportForm = ref({
  time: "",
});
const exportFormRef = ref();
const showExportDialog = (isShow = true) => {
  dialogVisible.value = isShow;
};



const detailDialog = ref(false);
const curRow = ref({});

//    * 支付状态 0-待支付 1-已支付
const payStatusMap = ref({
  0: "未支付",
  1: "已支付",
});
//    * 0-已关闭 10-待支付 20-待服务 25-申请退款 30-待验收 40-已完成 50-待评价 60-已退款
const statusMap = ref({
  0: "已关闭",
  10: "待支付",
  20: "待服务",
  25: "申请退款",
  30: "待验收",
  40: "已完成",
  50: "待评价",
  60: "已退款",
});


onMounted(() => {
  getList();
});

/** 查询商城订单列表 */
function getList() {
  loading.value = true;
  getMallOrderPage(queryParams).then(({ data }) => {
    mallOrderList.value = data.records;
    total.value = data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

const handleDetail = (row) => {
  curRow.value = row;
  detailDialog.value = true;
};
function handleRefund(row) {
  ElMessageBox.confirm("确定要退款吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    console.log(row);
    applyRefund({ id: row.id }).then(({ data }) => {
      if (data) {
        ElMessage.success("退款成功");
        getList();
      } else {
        ElMessage.error("退款失败");
      }
    });
  });
}

function handlePayEnd(row) {
  console.log(row);
  ElMessageBox.confirm("确定要核销吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    verificationOrder({ id: row.id }).then(({ data }) => {
      if (data) {
        ElMessage.success("核销成功");
        getList();
      } else {
        ElMessage.error("核销失败");
      }
    });
  });
}

const exportPropertyPayedFee1 = () => {
  exportFormRef.value.validate((valid) => {
    if (valid) {
      exportPropertyBill1({
        startTime: exportForm.value.time[0],
        endTime: exportForm.value.time[1],
      }).then((res) => {
        showExportDialog(false);
      });
    }
  });
};
</script>

<style scoped></style>
